import java.util.List;

public class dp {
    public int minimumTotal(List<List<Integer>> triangle) {
        int n = triangle.size();
        int[][] a = new int[n][n];

        a[0][0] = triangle.get(0).get(0);

        for (int i = 1; i < n; i++) {
            for (int j = 0; j <= i; j++) {
                if (j == 0) {
                    a[i][j] = triangle.get(i).get(j) + a[i - 1][j];
                } else if (j == i){
                    a[i][j] = triangle.get(i).get(j) + a[i - 1][j - 1];
                } else {
                    if (a[i][j - 1] < a[i][j]) {
                        a[i][j] = triangle.get(i).get(j) + a[i - 1][j - 1];
                    } else {
                        a[i][j] = triangle.get(i).get(j) + a[i - 1][j];
                    }
                }
            }
        }

        int ret = Integer.MAX_VALUE;
        for (int i = 0; i < n; i++) {
            Math.min(ret, a[n - 1][i]);
        }

        return ret;
    }
}
